// ========== BANNER - 广告参数获取模块 ==========
const {deviceBrand, deviceModel, deviceId, osName, osVersion} = uni.getSystemInfoSync();

// imei、idfa、deviceId、deviceModel、os、oaid、mac、aaid

const AdvUtil = {
	getDeviceInfo: async (type) => {
		try {
		  const deviceInfo = await new Promise((resolve, reject) => {
			plus.device.getInfo({
			  success: resolve,
			  fail: reject
			});
		  });
		  if(type === "imei"){
			  let imei = deviceInfo.imei || deviceInfo.uuid;
			  if (imei) {
				const imeiArr = imei.split(',');
				return imeiArr[0];
			  }
			  return '';
		  }else if(type === "idfa") {
			  return deviceInfo.idfa??''
		  }
		  return ''
		} catch (error) {
		  return '';
		}
	},
	getSystemInfo: (type) => {
		if(type === 'deviceId') {
			return deviceId
		}else if(type === 'deviceModel') {
			return deviceModel
		}else if(type === 'os'){
			return osName
		}
		return ''
	},
	getOaid: async () => {
		try {
			const oaidInfo = await new Promise((resolve, reject) => {
				plus.device.getOAID({
					success: resolve,
					fail: reject
				});
			});
			return oaidInfo.oaid??''
		} catch (error) {
			return '';
		}
	},
	getAaid: async (type) => {
		try {
			const aaidInfo = await new Promise((resolve, reject) => {
				plus.device.getAAID({
					success: resolve,
					fail: reject
				});
			});
			return aaidInfo.aaid??''
		} catch (error) {
			return '';
		}
	},
	getDeviceMac: () => {
		//获取本机Mac地址
		try{
			var deviceMac = '';
			var net = plus.android.importClass('java.net.NetworkInterface');
			var wl0 = net.getByName('wlan0');
			var macByte = wl0.getHardwareAddress();
			deviceMac = '';
			for (var i = 0; i < macByte.length; i++) {
				var tmp = '';
				var num = macByte[i];
				if (num < 0) {
					tmp = (255 + num + 1).toString(16);
				} else {
					tmp = num.toString(16);
				}
				if (tmp.length == 1) {
					tmp = '0' + tmp;
				}
				deviceMac += tmp;
			}
			return deviceMac??""
		}catch(error){
			return ""
		}
	},

};

export default AdvUtil;
